<template>
  <div>
    <appheader></appheader>
    <appheadernav></appheadernav>
    <div class="commonwidth">
      <div class="cart">
        <table>
          <thead>
            <tr>
              <th width="120">
                <div class="xtx-checkbox box">
                  <input
                    type="checkbox"
                    v-model="selectAll"
                    @change="checkAll"
                  />
                  <span>全选</span>
                </div>
              </th>
              <th width="400">商品信息</th>
              <th width="220">单价</th>
              <th width="180">数量</th>
              <th width="180">小计</th>
              <th width="140">操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-if="$store.state.cart.countInfo.length == 0">
              <td colspan="6">
                <div class="cart-none">
                  <img
                    src=""
                    alt=""
                  />
                  <p>购物车内暂时没有商品</p>
                  <div class="btn">
                    <button
                      class="xtx-button ellipsis middle primary"
                      @click="clk"
                    >
                      继续逛逛
                    </button>
                  </div>
                </div>
              </td>
            </tr>
            <tr
              v-else
              v-for="(item, index) in $store.state.cart.countInfo"
              :key="index"
            >
              <td>
                <div class="xtx-checkbox box">
                  <input
                    type="checkbox"
                    v-model="item.selected"
                    @change="ckeckOne(item)"
                  />
                </div>
              </td>
              <td>
                <div class="goods">
                  <a href="javascript:;" class="">
                    <img :src="item.picture" alt="" />
                  </a>
                  <div>
                    <p class="name ellipsis">{{ item.name }}</p>
                    <div class="cart-sku">
                      <div class="attrs">
                        <span class="ellipsis">{{ item.attrsText }}</span>
                        <i class="iconfont icon-angle-down"></i>
                      </div>
                    </div>
                  </div>
                </div>
              </td>
              <td class="tc">
                <p>{{ item.price }}</p>
              </td>
              <td class="tc">
                <div class="xtx-numbox">
                  <div class="numbox">
                    <button @click="jian(item)">-</button>
                    <input type="text" :value="item.count" />
                    <button @click="jia(item)">+</button>
                  </div>
                </div>
              </td>
              <td class="tc">
                <p class="f16 red">¥{{ item.nowPrice }}</p>
              </td>
              <td class="tc">
                <p><a href="javascript:;">移入收藏夹</a></p>
                <p>
                  <a class="green" href="javascript:;" @click="del(index)"
                    >删除</a
                  >
                </p>
                <p><a href="javascript:;">找相似</a></p>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <div class="action">
        <div class="batch">
          <div class="xtx-checkbox box">
            <input type="checkbox" v-model="selectAll" @change="checkAll" />
            <span>全选</span>
          </div>
          <a href="javascript:;" @click="delAll">删除商品</a>
          <a href="javascript:;">移入收藏夹</a>
          <a href="javascript:;">清空失效商品</a>
        </div>
        <div class="total">
          共 {{ $store.getters.getcount }} 件商品，已选择
          {{ $store.getters.getcounts }} 件，商品合计：
          <span class="red">¥{{ $store.getters.getmoney }}</span
          ><button class="xtx-button ellipsis middle primary">下单结算</button>
        </div>
      </div>
    </div>
    <appfooter></appfooter>
  </div>
</template>
<script>
import appheader from "@/page/index/header.vue";
import appheadernav from "@/page/index/headerNav.vue";
import appfooter from "@/page/index/foooter.vue";
export default {
  components: {
    appheader,
    appheadernav,
    appfooter,
  },
  data() {
    return {
      selectAll: false,
    };
  },
  mounted() {
    console.log(this.$store.state.login.logo.token);
    if (this.$store.state.login.logo.token) {
      this.getcart();
    }
  },
  methods: {
    getcart() {
      this.axios
        .get("https://apipc-xiaotuxian-front.itheima.net/member/cart", {
          headers: {
            Authorization: "Bearer " + this.$store.state.login.logo.token,
          },
        })
        .then((res) => {
          if (res.data.msg == "操作成功") {
            var bool = true;
            res.data.result.forEach((i) => {
              i.selected == false && (bool = false);
            });
            this.selectAll = bool;
            this.$store.commit("counts", res.data.result);
          }
        });
    },
    checkAll() {
      var ids = [];
      this.$store.state.cart.countInfo.forEach((i) => {
        ids.push(i.skuId);
      });
      console.log(ids);
      this.axios
        .put(
          "https://apipc-xiaotuxian-front.itheima.net/member/cart/selected",
          { ids: ids, selected: this.selectAll },
          {
            headers: {
              Authorization: "Bearer " + this.$store.state.login.logo.token,
            },
          }
        )
        .then((res) => {
          if (res.data.msg == "操作成功") {
            this.getcart();
          }
        });
    },
    ckeckOne(i) {
      var bool = true;
      this.$store.state.cart.countInfo.forEach((i) => {
        i.selected == false && (bool = false);
      });
      this.selectAll = bool;
      var id = i.skuId;
      console.log(id);
      this.axios
        .put(
          `https://apipc-xiaotuxian-front.itheima.net/member/cart/${id}`,
          {
            selected: this.selectAll,
          },
          {
            headers: {
              Authorization: "Bearer " + this.$store.state.login.logo.token,
            },
          }
        )
        .then((res) => {
          if (res.data.msg == "操作成功") {
            this.getcart();
          }
        });
    },
    jian(i) {
      if (i.count > 1) {
        var count = i.count;
        count--;
        var id = i.skuId;
        this.axios
          .put(
            `https://apipc-xiaotuxian-front.itheima.net/member/cart/${id}`,
            {
              count: count,
            },
            {
              headers: {
                Authorization: "Bearer " + this.$store.state.login.logo.token,
              },
            }
          )
          .then((res) => {
            if (res.data.msg == "操作成功") {
              this.getcart();
            }
          });
      }
    },
    jia(i) {
      var count = i.count;
      count++;
      var id = i.skuId;
      this.axios
        .put(
          `https://apipc-xiaotuxian-front.itheima.net/member/cart/${id}`,
          {
            count: count,
          },
          {
            headers: {
              Authorization: "Bearer " + this.$store.state.login.logo.token,
            },
          }
        )
        .then((res) => {
          if (res.data.msg == "操作成功") {
            this.getcart();
          }
        });
    },
    del(i) {
      var id = this.$store.state.cart.countInfo[i].skuId;
      console.log(id);
      this.axios({
        url: "https://apipc-xiaotuxian-front.itheima.net/member/cart",
        data: { ids: [id] },
        headers: {
          Authorization: "Bearer " + this.$store.state.login.logo.token,
        },
        method: "delete",
      }).then((res) => {
        if (res.data.msg == "操作成功") {
          this.getcart();
        }
      });
    },
    delAll() {
      var a = [];
      this.$store.state.cart.countInfo.forEach((i) => {
        if (i.selected == true) {
          a.push(i.skuId);
        }
      });
      console.log(a);
      this.axios({
        url: "https://apipc-xiaotuxian-front.itheima.net/member/cart",
        data: { ids: a },
        headers: {
          Authorization: "Bearer " + this.$store.state.login.logo.token,
        },
        method: "delete",
      }).then((res) => {
        if (res.data.msg == "操作成功") {
          this.getcart();
        }
      });
    },
    clk() {
      this.$router.push("/");
    },
  },
  //进入当前组件的路由页面之前触发
  beforeRouteEnter(to, from, n) {
    // console.log(to, from);
    n();
  },
  //离开路由页面时触发
  beforeRouteLeave(to, from, n) {
    console.log(to, from);
    n();
  },
  
};
</script>
<style lang="scss" scoped>
@import url(./index.css);
.goods-relevant {
  background: #fff;
  min-height: 460px;
  margin-top: 20px;
}

.cart {
  background: #fff;
  color: #666;
  margin-top: 20px;
}

thead {
  height: 71px;
}

tbody {
  height: 121px;
}
.primary {
  border-color: #27ba9b;
  background: #27ba9b;
  color: #fff;
}
.ellipsis {
  width: 180px;
  height: 50px;
  font-size: 16px;
  text-align: center;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
  outline: none;
  appearance: none;
}
.batch {
  display: flex;
  justify-content: start;
}

.goods-relevant .header {
  height: 80px;
  line-height: 80px;
  padding: 0 20px;
}

.goods-relevant .header .title {
  font-size: 20px;
  padding-left: 10px;
}

.goods-relevant .header .icon {
  width: 16px;
  height: 16px;
  display: inline-block;
  border-top: 4px solid #27ba9b;
  border-right: 4px solid #27ba9b;
  box-sizing: border-box;
  position: relative;
  transform: rotate(45deg);
}

.goods-relevant .header .icon:before {
  content: "";
  width: 10px;
  height: 10px;
  position: absolute;
  left: 0;
  top: 2px;
  background: #bcf1e6;
}

.xtx-carousel {
  height: 380px;
}

.xtx-carousel .carousel-indicator {
  bottom: 30px;
}

.xtx-carousel .carousel-indicator span.active {
  background: #27ba9b;
}

.xtx-carousel .carousel-btn {
  top: 110px;
  opacity: 1;
  background: transparent;
  color: #ddd;
}

.xtx-carousel .carousel-btn i {
  font-size: 30px;
}

.cart-none {
  text-align: center;
  padding: 150px 0;
  background: #fff;
}

.cart-none img {
  width: 180px;
}

.cart-none p {
  color: #999;
  padding: 20px 0;
}

.goods-sku {
  padding-left: 10px;
  padding-top: 20px;
}

.goods-sku dl {
  display: flex;
  padding-bottom: 10px;
  align-items: center;
}

.goods-sku dl dt {
  width: 50px;
  color: #999;
}

.goods-sku dl dd {
  flex: 1;
  color: #666;
  line-height: 40px;
}

.goods-sku dl dd > img {
  width: 50px;
  height: 50px;
  margin-bottom: 5px;
  border: 1px solid #e4e4e4;
  margin-right: 10px;
  cursor: pointer;
}

.goods-sku dl dd > img.selected {
  border-color: #27ba9b;
}

.goods-sku dl dd > img.disabled {
  opacity: 0.6;
  border-style: dashed;
  cursor: not-allowed;
}

.goods-sku dl dd > span {
  display: inline-block;
  height: 30px;
  line-height: 28px;
  padding: 0 20px;
  border: 1px solid #e4e4e4;
  margin-right: 10px;
  cursor: pointer;
}

.goods-sku dl dd > span.selected {
  border-color: #27ba9b;
}

.goods-sku dl dd > span.disabled {
  opacity: 0.6;
  border-style: dashed;
  cursor: not-allowed;
}

.cart-sku {
  height: 28px;
  border: 1px solid #f5f5f5;
  padding: 0 6px;
  position: relative;
  margin-top: 10px;
  display: inline-block;
}

.cart-sku .attrs {
  line-height: 24px;
  display: flex;
}

.cart-sku .attrs span {
  max-width: 230px;
  font-size: 14px;
  color: #999;
}

.cart-sku .attrs i {
  margin-left: 5px;
  font-size: 14px;
}

.cart-sku .layer {
  position: absolute;
  left: -1px;
  top: 40px;
  z-index: 10;
  width: 400px;
  border: 1px solid #27ba9b;
  box-shadow: 2px 2px 4px #e6faf6;
  background: #fff;
  border-radius: 4px;
  font-size: 14px;
  padding: 20px;
}

.tc {
  text-align: center;
}

.tc .xtx-numbox {
  margin: 0 auto;
  width: 120px;
}

.numbox {
  display: flex;
  justify-content: space-around;

  input {
    border: 1px solid #ccc;
    width: 60px;
    text-align: center;
  }

  button {
    border: 0;
    padding: 5px 15px;
    cursor: pointer;
  }
}

.red {
  color: #cf4444;
}

.green {
  color: #27ba9b;
}

.f16 {
  font-size: 16px;
}

.goods {
  display: flex;
  align-items: center;
}

.goods img {
  width: 100px;
  height: 100px;
}

.goods > div {
  width: 280px;
  font-size: 16px;
  padding-left: 10px;
}

.goods > div .attr {
  font-size: 14px;
  color: #999;
}

.action {
  display: flex;
  background: #fff;
  margin-top: 20px;
  height: 80px;
  align-items: center;
  font-size: 16px;
  justify-content: space-between;
  padding: 0 30px 0 0;
}

.action .xtx-checkbox {
  color: #999;
}

.box {
  display: flex;
  padding-left: 30px;

  input {
    margin-right: 5px;
  }
}

.action .batch a {
  margin-left: 20px;
}

.action .red {
  font-size: 18px;
  margin-right: 20px;
  font-weight: 700;
}

.tit {
  color: #666;
  font-size: 16px;
  font-weight: 400;
  line-height: 50px;
}

.xtx-cart-page .cart {
  background: #fff;
  color: #666;
}

.xtx-cart-page .cart table {
  border-spacing: 0;
  border-collapse: collapse;
  line-height: 24px;
}

.xtx-cart-page .cart table td,
.xtx-cart-page .cart table th {
  padding: 10px;
  border-bottom: 1px solid #f5f5f5;
}

.xtx-cart-page .cart table td:first-child,
.xtx-cart-page .cart table th:first-child {
  text-align: left;
  padding-left: 30px;
  color: #999;
}

.xtx-cart-page .cart table th {
  font-size: 16px;
  font-weight: 400;
  line-height: 50px;
}
</style>